//
//  UIBarButtonItem+extension.m
//  OCsinaBlog
//
//  Created by jobo on 16/4/24.
//  Copyright © 2016年 jobo. All rights reserved.
//

#import "UIBarButtonItem+extension.h"

@implementation UIBarButtonItem (extension)

//设置图片导航栏按钮
+(instancetype)barButtonItemWithNolImg : (NSString *) nolImg withHlgImg : (NSString *) hlgImg withTarget:(id)target action:(SEL)action
{
    UIButton *addfriendBtn = [UIButton buttonWithType:UIButtonTypeCustom] ;
    
    UIImage *img = [UIImage imageNamed:nolImg];
    
    [addfriendBtn setImage:[UIImage imageNamed:nolImg] forState:UIControlStateNormal] ;
    [addfriendBtn setImage:[UIImage imageNamed:hlgImg] forState:UIControlStateHighlighted] ;
    
    addfriendBtn.bounds = CGRectMake(0, 0, img.size.width, img.size.height);
    //按钮响应事件
    [addfriendBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[UIBarButtonItem alloc] initWithCustomView: addfriendBtn] ;
}
//设置文本导航栏按钮
+(instancetype)barButtonWithTitle:(NSString *)title withTarget:(id)target action:(SEL)action
{
    
    UIFont *titleFont = [UIFont systemFontOfSize : 16] ;
  
    CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    
    CGSize titleSize = [title boundingRectWithSize:maxSize options:NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName : titleFont} context:nil].size;
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem] ;
    
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
    
    [btn setTitle:title forState:UIControlStateNormal];
    
    btn.bounds = CGRectMake(0, 0, titleSize.width , titleSize.height) ;
    
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside] ;
    
    btn.titleLabel.font = titleFont ;
    
    return [[UIBarButtonItem alloc] initWithCustomView: btn] ;


}
//设置图文导航栏按钮
+(instancetype)barButtonWithBackTitle:(NSString *)title andWithNolImg : (NSString *)nolImg withHlgImg :(NSString *)hlgImg withTarget:(id)target action:(SEL)action
{
    
    NSString *backTitle = title;
    UIImage *backImg = [UIImage imageNamed:nolImg] ;
    
    //计算文字大小
    UIFont *titleFont = [UIFont systemFontOfSize : 16] ;
    
    CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    
    CGSize titleSize = [backTitle boundingRectWithSize:maxSize options:NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName : titleFont} context:nil].size;
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom] ;
    
    [backBtn setTitle:backTitle forState:UIControlStateNormal] ;
    
    [backBtn setImage:[UIImage imageNamed:nolImg] forState:UIControlStateNormal] ;
    [backBtn setImage:[UIImage imageNamed:hlgImg] forState:UIControlStateHighlighted];
    [backBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    
    backBtn.bounds = CGRectMake(0, 0, backImg.size.width + titleSize.width, backImg.size.height) ;
    
    backBtn.titleLabel.font = titleFont ;
    
    //为按钮添加事件
    [backBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; 
    
    return [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
    
}

@end
